﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WCFServiceWebRole1
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        Accnt mem;
        //dbmanager db = new dbmanager();

        protected void Page_Load(object sender, EventArgs e)
        {
            mem = (Accnt)Session["userSession"];
            if (mem != null)
            {
                lblUser.Text = mem.Name;
                loginProfile.Visible = false;
                userProfile.Visible = true;
                if (mem.Role.ToLower() == "admin")
                {
                    panelAdmin.Visible = true;
                }
            }


        }

        protected void Login_Click(object sender, EventArgs e)
        {
            //S2501681E //123
            //dbmanager db = new dbmanager();
            if (NRICCheck(tbxUser.Text))
            {
                string password = tbxPass.Text.Trim();
                Service1 svc = new Service1();
                mem = svc.LoginUser(tbxUser.Text, password);
                if (mem != null)
                {
                    Session["userSession"] = mem;
                    lblUser.Text = mem.Name;
                    loginProfile.Visible = false;
                    userProfile.Visible = true;
                    if (mem.Role.ToLower() == "admin")
                    {
                        panelAdmin.Visible = true;
                    }
                }
            }
            else
            {
                //wrong password
            }
        }

        private bool NRICCheck(string nric)
        {
            char letter = '-';
            char[] spitledNRIC = nric.ToUpper().ToCharArray();
            if (spitledNRIC.Count() == 9)
            {
                string input = (" " + nric[1] + nric[2] + nric[3] + nric[4] + nric[5] + nric[6] + nric[7]).Trim();
                int sum = 0;
                int remainder;
                int[] weight = new int[7] { 2, 7, 6, 5, 4, 3, 2 };

                char[] resultSET1 = new char[11] { 'J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' };
                char[] resultSET2 = new char[11] { 'X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K' };

                for (int i = 0; i < input.Length; i++)
                {
                    sum += int.Parse(input[i].ToString()) * weight[i];
                }

                if (spitledNRIC[0].ToString().Equals("T") || spitledNRIC[0].ToString().Equals("G"))
                    sum += 4;

                remainder = sum % 11;

                if (spitledNRIC[0].ToString().Equals("S") || spitledNRIC[0].ToString().Equals("T"))
                    letter = resultSET1[remainder];
                else
                    letter = resultSET2[remainder];

                if (spitledNRIC[8] != letter)
                {
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session["userSession"] = null;
            lblUser.Text = null;
            loginProfile.Visible = true;
            userProfile.Visible = false;
            panelAdmin.Visible = false;
        }

    }
}